#include "head.h"

/* Usage: 服务端 */

void do_rm(int net_fd, self_defined_pro_t *protect){
    //先接收客户端的信息message
    recv(net_fd, protect, sizeof(self_defined_pro_t), MSG_WAITALL);
    //处理接收的信息数组
    char *token;
    char user_path[100] = {0};
    char client_path[1024] = {0};
    char path[1024] = {0};

    //开始拼接路径
    const char *delim = "/";
    token = strtok(protect->message, delim);
    token = strtok(protect->message, delim);
    //拼接用户
    strncpy(user_path, token, strlen(token));
    strncpy(user_path, token, strlen(token));
    strcat(path, user_path);
    strcat(path, "/");
    strncpy(client_path, protect->message, strlen(protect->message));
    DIR *user_fd = opendir(path);
    if(user_fd){
        //判断是文件的话是否存在
        int ret_path_file = open(strcat(path, client_path), O_RDWR);
        if(ret_path_file){
            protect->cmd = 4;
            protect->para_num = 2;
            send(net_fd, protect, sizeof(self_defined_pro_t), MSG_NOSIGNAL);
            return;
        }
        close(ret_path_file);
        //判断是目录的话是否存在
        DIR *client_fd = opendir(strcat(path, client_path));
        if(client_fd){
            protect->cmd = 4;
            protect->para_num = 2;
            send(net_fd, protect, sizeof(self_defined_pro_t), MSG_NOSIGNAL);
            return;
        }
        else{
            protect->cmd =60;
            send(net_fd, protect, sizeof(self_defined_pro_t), MSG_NOSIGNAL);
            return;
        }
        closedir(client_fd);
        return;
    }
    closedir(user_fd);
    return;
}


